//
// Decompiled by Jadx - 479ms
//
package ed;

import android.app.Application;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;
import d.f;
import dq.a;
import java.lang.reflect.Array;
import java.lang.reflect.Method;

public final class b {
    /* JADX WARN: Code restructure failed: missing block: B:18:0x0034, code lost:
    
        if (r8 == null) goto L21;
     */
    /* JADX WARN: Code restructure failed: missing block: B:5:0x0029, code lost:
    
        if (r8 != null) goto L20;
     */
    /* JADX WARN: Code restructure failed: missing block: B:6:0x0039, code lost:
    
        return null;
     */
    /* JADX WARN: Code restructure failed: missing block: B:8:0x0036, code lost:
    
        r8.close();
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:23:0x003f  */
    /* JADX WARN: Type inference failed for: r7v0 */
    /* JADX WARN: Type inference failed for: r7v1, types: [android.database.Cursor] */
    /* JADX WARN: Type inference failed for: r7v2 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static String a(Application application, Uri uri, String str, String[] strArr) {
        IllegalArgumentException e;
        Cursor cursor;
        ?? r7 = 0;
        try {
            try {
                cursor = application.getContentResolver().query(uri, new String[]{"_data"}, str, strArr, null);
                if (cursor != null) {
                    try {
                        if (cursor.moveToFirst()) {
                            String string = cursor.getString(cursor.getColumnIndexOrThrow("_data"));
                            cursor.close();
                            return string;
                        }
                    } catch (IllegalArgumentException e2) {
                        e = e2;
                        e.printStackTrace();
                    }
                }
            } catch (Throwable th) {
                r7 = application;
                th = th;
                if (r7 != 0) {
                    r7.close();
                }
                throw th;
            }
        } catch (IllegalArgumentException e3) {
            e = e3;
            cursor = null;
        } catch (Throwable th2) {
            th = th2;
            if (r7 != 0) {
            }
            throw th;
        }
    }

    public static String b(Uri uri) {
        Uri uri2;
        long j;
        boolean z;
        String str;
        a.n();
        Application application = a.b;
        if (application != null) {
            String scheme = uri.getScheme();
            if (!"file".equals(scheme)) {
                "content".equals(scheme);
                if ("content".equals(scheme)) {
                    int i = Build.VERSION.SDK_INT;
                    if (DocumentsContract.isDocumentUri(application, uri)) {
                        if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
                            String[] split = DocumentsContract.getDocumentId(uri).split(":");
                            if (split.length == 2) {
                                String str2 = split[0];
                                if ("primary".equalsIgnoreCase(str2)) {
                                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                                }
                                StorageManager storageManager = (StorageManager) application.getSystemService("storage");
                                try {
                                    Class<?> cls = Class.forName("android.os.storage.StorageVolume");
                                    Method method = storageManager.getClass().getMethod("getVolumeList", new Class[0]);
                                    Method method2 = cls.getMethod("getUuid", new Class[0]);
                                    Method method3 = cls.getMethod("getState", new Class[0]);
                                    Method method4 = cls.getMethod("getPath", new Class[0]);
                                    Method method5 = cls.getMethod("isPrimary", new Class[0]);
                                    Method method6 = cls.getMethod("isEmulated", new Class[0]);
                                    Object invoke = method.invoke(storageManager, new Object[0]);
                                    int length = Array.getLength(invoke);
                                    for (int i2 = 0; i2 < length; i2++) {
                                        Object obj = Array.get(invoke, i2);
                                        if (!"mounted".equals(method3.invoke(obj, new Object[0])) && !"mounted_ro".equals(method3.invoke(obj, new Object[0]))) {
                                            z = false;
                                            if (z && ((!((Boolean) method5.invoke(obj, new Object[0])).booleanValue() || !((Boolean) method6.invoke(obj, new Object[0])).booleanValue()) && (str = (String) method2.invoke(obj, new Object[0])) != null && str.equals(str2))) {
                                                return method4.invoke(obj, new Object[0]) + "/" + split[1];
                                            }
                                        }
                                        z = true;
                                        if (z) {
                                            return method4.invoke(obj, new Object[0]) + "/" + split[1];
                                        }
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
                            String documentId = DocumentsContract.getDocumentId(uri);
                            if (!TextUtils.isEmpty(documentId)) {
                                if (documentId.startsWith("raw:")) {
                                    return documentId.substring(4);
                                }
                                if (documentId.startsWith("msf:") && i >= 29) {
                                    String[] split2 = documentId.split(":");
                                    if (split2.length == 2) {
                                        return a(application, f.c(), "_id=?", new String[]{split2[1]});
                                    }
                                }
                                String str3 = d.a;
                                try {
                                    j = Long.parseLong(documentId);
                                } catch (NumberFormatException e2) {
                                    e2.printStackTrace();
                                    j = -1;
                                }
                                if (j != -1) {
                                    return a(application, ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), j), null, null);
                                }
                            }
                        } else if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
                            String[] split3 = DocumentsContract.getDocumentId(uri).split(":");
                            String str4 = split3[0];
                            if ("image".equals(str4)) {
                                uri2 = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                            } else if ("video".equals(str4)) {
                                uri2 = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                            } else if ("audio".equals(str4)) {
                                uri2 = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                            } else {
                                uri2 = null;
                            }
                            return a(application, uri2, "_id=?", new String[]{split3[1]});
                        }
                    }
                }
                if ("content".equalsIgnoreCase(scheme)) {
                    if ("com.google.android.apps.photos.content".equals(uri.getAuthority())) {
                        return uri.getLastPathSegment();
                    }
                    if ("com.huawei.hidisk.fileprovider".equals(uri.getAuthority())) {
                        String path = uri.getPath();
                        if (!d.k(path) && path.startsWith("/root")) {
                            return path.replace("/root", "");
                        }
                    } else if ("com.tencent.mtt.fileprovider".equals(uri.getAuthority())) {
                        String path2 = uri.getPath();
                        if (!d.k(path2)) {
                            return Environment.getExternalStorageDirectory() + path2.substring(10);
                        }
                    }
                    return a(application, uri, null, null);
                }
                if (!"file".equalsIgnoreCase(scheme)) {
                    return null;
                }
            }
            return uri.getPath();
        }
        return null;
    }
}
